'use strict';

const BaseService = require('../base');
const { Op } = require('sequelize');
class TicketShopService extends BaseService {
  static get modelName() {
    return 'Business.TicketShop';
  }

  async login(username, password) {
    const { ctx, service } = this;
    const result = await service.business.ticketShop.findOne({
      where: {
        username,
        password
      }
    });
    return result;
  }
  async beforeCreate(data) {
    // 判断username是否存在
    const { ctx, service } = this;
    if (!data.username) return true
    const result = await this.ctx.model.Business.TicketShop.findOne({
      where: {
        username: data.username
      }
    });
    if (result) {
      throw new Error('商家账号重复');
    }
    return  true
  }

  async beforeUpdate(id, data) {
    // 判断username是否存在
    const {ctx, service} = this;
    if (!data.username) return true
    const result = await this.ctx.model.Business.TicketShop.findOne({
      where: {
        username: data.username,
        id: {
          [Op.ne]: data.id
        }
      }
    });
    if (result) {
      throw new Error('商家账号重复');
    }
    return true
  }
}

module.exports = TicketShopService;
